package locations.actions;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import locations.dao.DAOFactory;
import locations.model.Institution;
import locations.model.Pacient;
import locations.util.DistanceToCoordMap;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

public class FindNearestInstitutionsAction extends ActionSupport implements ServletRequestAware, ServletResponseAware {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request; 
	private List<Institution> institutions = new ArrayList<Institution>();
	private Pacient pacient;
	private Map<Double, String> map;

	public String execute() throws Exception {
		
		Long   pacientId = Long.parseLong(request.getParameter("id"));
		Double delta = Double.parseDouble(request.getParameter("delta"));
		
		this.pacient = DAOFactory.getPacientDAO().retrieveById(pacientId);
		this.institutions = DAOFactory.getInstitutionDAO().findInstitutionsNearTo(pacient.getLocation(), delta);
		this.map = DistanceToCoordMap.getMap();
		return SUCCESS;
	}
	
	public Map<Double, String> getMap() {
		return map;
	}

	public void setMap(Map<Double, String> map) {
		this.map = map;
	}	
	public List<Institution> getInstitutions() {
		return institutions;
	}

	public void setInstitutions(List<Institution> institutions) {
		this.institutions = institutions;
	}
	
	public Pacient getPacient()
	{
		return this.pacient;
	}
	public void setPacient(Pacient p)
	{
		this.pacient = p;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	
}
